<template>
  <view class="page">
    <customtop :top_title="top_title"></customtop>
    <view class="top_search" :style="{top: 54  + statusHeight+ 'px'}">
      <view class="container">
        <view class="top_search_box">
          <view class="icon"><i class="iconfont icon-sousuoxiao"></i></view>
          <view class="input">
            <input class="weui-input" type="text" placeholder="请输入搜索内容" v-model="params.key" @blur="doSearch" />
          </view>
        </view>
      </view>
    </view>
    <view class="top_search_blank"></view>
    <view class="abb_list" v-if="!data_list_loading">
      <view v-if="data_list.length > 0">
        <view class="items">
          <view class="item" v-for="(item, index) in data_list" :key="index" @click="
                jumpPage(
                  logined
                    ? '/pages/postList/detail?id=' + item.id
                    : '/pages/account/login_password'
                )
              ">
            <view class="title">{{item.title}}</view>
            <view class="typesa">
              <view class="bitem">
                <span class="xb-1" v-if="item.issyxm == 0">项目</span>
                <span class="xb on" v-if="item.issyxm > 0">上押项目</span>
              </view>
              <view class="bitem"><i class="iconfont icon-chakan"></i><span class="txt">{{item.cksl}}</span></view>
              <view class="bitem"><i class="iconfont icon-icon-person-hezuo2"></i><span class="txt">{{item.zxsl}}</span>
              </view>
              <view class="bitem bitem4" v-if="item.iszhiding == 1"><span class="xb xb1">置顶</span></view>
            </view>
          </view>
        </view>
        <view class="uloadmore"><u-loadmore :status="loadmore_status" /></view>
      </view>
      <view :style="{ marginTop: '30px' }" v-if="data_list.length == 0">
        <u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无内容~">
        </u-empty>
      </view>
    </view>
  </view>
  </view>
</template>

<script>
  import {
    request,
    upload
  } from "@/utils/http.js"
  import customtop from "@/components/customtop.vue"
  export default {
    components: {
      customtop
    },

    data() {
      return {
        loading: true,
        data_list: [],
        data_list_loading: true,
        loadmore_status: 'loadmore',
        loadmore_finished: false,
        params: {
          page_size: 15,
          page: 1,
          key: '',
        },
        logined: false,
        loginUser: {},
        statusHeight: 0,
        top_title: null,
        id:null
      }
    },

    onLoad: function(options) {
      // this.getInit();
      this.statusHeight = uni.getSystemInfoSync().statusBarHeight;
      this.id = options.ctn
      switch (options.ctn) {
        case '1':
          this.top_title = '最新发布';
          break;
        case '2':
          this.top_title = '最热项目';
          break;
        case '3':
          this.top_title = '重金上押';
          break;
        case 4:
          this.top_title = '担保交易';
          break;
        default:
          break;
      }
      this.getAbbsPaginate()
    },

    onShow: function() {
      this.getUserInfo()
    },

    // 加载更多
    onReachBottom() {
      this.getMore();
    },

    // 下拉刷新
    onPullDownRefresh: function() {
      this.params.page = 1;
      this.getAbbsPaginate();
    },

    methods: {
      getUserInfo() {
        request.post('/user/getLoginUser').then(res => {
          if (res.data.id) {
            this.logined = true;
            this.loginUser = res.data;
          } else {
            this.logined = false;
          }
        })
      },
      // 请求接口，获取列表数据
      getAbbsPaginate() {
        this.data_list_loading = true;
        console.log(this.id);
        request.post('/xiangmushe/getIndexAllList', {...this.params,paixu:this.id == '1'? 1 : 2,issyxm:this.id == '3' ? 1 : ""}).then(res => {
          uni.stopPullDownRefresh();
          uni.hideLoading();
          this.data_list_loading = false;
          // 返回数据为空
          if (res.data.total == 0) {
            this.data_list = [];
            return false;
          }

          // 组装数据
          if (res.data.current_page == 1) {
            this.data_list = res.data.data;
          } else {
            this.data_list = this.data_list.concat(res.data.data);
          }

          // 最后一页
          if (this.params.page == res.data.last_page) {
            this.loadmore_finished = true;
            this.loadmore_status = 'nomore';
            return false;
          }

          this.params.page = parseInt(res.data.current_page) + parseInt(1);
          this.loadmore_status = 'loadmore';
          this.loadmore_finished = false;
        })
      },

      // 初始化请求
      getInit() {
        uni.showLoading()
        this.getAbbsPaginate();
      },

      // 加载更多
      getMore() {
        if (!this.loadmore_finished) {
          this.loadmore_status = 'loading';
          this.getAbbsPaginate();
        }
      },

      doSearch() {
        uni.showLoading();
        this.getInit();
      },

      switchTab(url) {
        uni.switchTab({
          url: url
        });
      },

      jumpPage(url) {
        uni.navigateTo({
          url: url
        });
      }
    }
  }
</script>

<style>
  @import url("abb.css");

  .page {
    padding-bottom: 30rpx;
  }
</style>
